package com.yn.bsc.web.api;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import com.yn.bsc.service.SiteHealthyDailyRecordService;
import com.yn.bsc.vo.SiteStationsVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yn.bsc.core.Result;
import com.yn.bsc.core.ResultGenerator;
import com.yn.bsc.model.SiteStations;
import com.yn.bsc.service.SiteStationsService;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 基站信息表 Created by ZuoLG on 2017/09/20.
 */
@RestController
@RequestMapping("/api/site/stations")
public class SiteStationsController extends BaseController<SiteStationsService, SiteStations> {
	@Resource
	private SiteStationsService siteStationsService;

	@Resource
	private SiteHealthyDailyRecordService healthyDailyRecordService;

	@SuppressWarnings("rawtypes")
	@PostMapping("/listByPage")
	public Result listByPage(@RequestParam(defaultValue = "0") Integer page,
			@RequestParam(defaultValue = "10") Integer size, SiteStations stations) {
		PageHelper.startPage(page, size);

		Condition condition = new Condition(SiteStations.class);
		Criteria criteria = condition.createCriteria();
		if (stations != null && stations.getSiteId() != null) {
			criteria.andEqualTo("siteId", stations.getSiteId());
		}
		if (stations != null && stations.getBscName() != null) {
			criteria.andEqualTo("bscName", stations.getBscName());
		}
		if (stations != null && stations.getNameCn() != null) {
			criteria.andEqualTo("nameCn", stations.getNameCn());
		}

		List<SiteStations> list = siteStationsService.findByCondition(condition);
		@SuppressWarnings("unchecked")
		PageInfo pageInfo = new PageInfo(list);
		return ResultGenerator.genSuccessResult(pageInfo);
	}

	@Override
	@SuppressWarnings("rawtypes")
	@PostMapping("/list")
	public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
		PageHelper.startPage(page, size);
		List<SiteStations> list = siteStationsService.findAll();
		@SuppressWarnings("unchecked")
		PageInfo pageInfo = new PageInfo(list);
		return ResultGenerator.genSuccessResult(pageInfo);
	}


	@SuppressWarnings("rawtypes")
	@PostMapping("/healthyList")
	public Result healthyList(@RequestParam(defaultValue = "0") int page,
							  @RequestParam(defaultValue = "140") int size,
							  @RequestParam String start,@RequestParam String end,
							  @RequestParam(defaultValue = "0") int type) {
		PageHelper.startPage(page, size);
		List<SiteStationsVo> siteList = siteStationsService.findAllGroupByCity(type);
		List<SiteStationsVo> unHealthSiteList = siteStationsService.findSiteByScoreGroupByCity(type,start,end);
		for (SiteStationsVo site : siteList){
			for (SiteStationsVo siteTemp : unHealthSiteList){
				if (site.getUrbanNameCn().equals(siteTemp.getUrbanNameCn()) &&
						site.getSuburbanNameCn().equals(siteTemp.getSuburbanNameCn())){
					site.setUnHealthSiteCount(siteTemp.getUnHealthSiteCount());
				}
			}
		}

		Collections.sort(siteList);
		for (int i = 0 ; i < siteList.size() ; i++){
			siteList.get(i).setRanking(i+1);
		}

		@SuppressWarnings("unchecked")
		PageInfo pageInfo = new PageInfo(siteList);
		return ResultGenerator.genSuccessResult(pageInfo);
	}
}
